/*
 * ThumbnailPanel.java
 *
 * Created on March 30, 2007, 1:36 PM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.slimserver.musicjukebox.lib.ArtManager;
import com.bff.bjj.ui.component.ColorProperties;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import edu.umd.cs.piccolo.PCanvas;
import edu.umd.cs.piccolo.activities.PActivity;
import edu.umd.cs.piccolo.nodes.PImage;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.MouseEvent;
import org.bff.slimserver.musicobjects.SlimPlayableItem;
import org.bff.slimserver.musicobjects.SlimPlayableObject;

/**
 *
 * @author  bfindeisen
 */
public class ThumbnailPanel extends PCanvas {

    private PImage pImage;
    private SlimPlayableItem currentSong;
    private SlimPlayableItem song;
    private static final int PADDING = 10;
    private PActivity imageLoadActivity;

    /** Creates new form ThumbnailPanel */
    public ThumbnailPanel() {
        setBackground(ColorProperties.PANEL_COVER_ART_BACKGROUND);
        removeInputEventListener(getPanEventHandler());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.FlowLayout());
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    public void loadImage(final SlimPlayableItem song) {
        loadImage(song, JukeboxProperties.getInstance().getImageDelayInMillis());
    }

    public void loadImage(final SlimPlayableItem song, final int delay) {
        loadImage(song, delay, true);
    }

    public void removeImage() {
        removeImage(JukeboxProperties.getInstance().getImageDelayInMillis());
    }

    public void removeImage(final int delay) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                PActivity activity = getCamera().animateToTransparency(0, delay == 0 ? 1 : delay);
                PActivity.PActivityDelegate delegate = new PActivity.PActivityDelegate() {

                    public void activityFinished(PActivity a) {
                        getLayer().removeChild(pImage);
                    }

                    @Override
                    public void activityStarted(PActivity arg0) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }

                    @Override
                    public void activityStepped(PActivity arg0) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
                };
            }
        }).start();
    }

    public void terminate() {
        if (getImageLoadActivity() != null) {
            getImageLoadActivity().terminate(PActivity.TERMINATE_WITHOUT_FINISHING);
        }
    }

    public void terminateAndFinish() {
        if (getImageLoadActivity() != null) {
            getImageLoadActivity().terminate();
        }
    }

    public void loadImage(final SlimPlayableItem item, final int delay, final boolean scale) {

        if (currentSong == null || currentSong.getImageUrl() == null ||
                currentSong.getAlbum() == null ||
                !currentSong.getAlbum().equals(item.getAlbum())) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        currentSong = item;
                        setSong(item);
                        
                       final Image image =  ArtManager.getArt(item);

                        terminate();

                        if (image == null) {
                            PActivity activity = getCamera().animateToTransparency(0, delay == 0 ? 1 : delay);
                            return;
                        }

                        if (pImage != null) {
                            setImageLoadActivity(getCamera().animateToTransparency(0, delay == 0 ? 1 : delay));
                            PActivity.PActivityDelegate delegate = new PActivity.PActivityDelegate() {

                                public void activityFinished(PActivity a) {
                                    getLayer().removeChild(pImage);
                                    createImage(image, delay == 0 ? 1 : delay, scale);
                                }

                                public void activityStarted(PActivity a) {
                                }

                                public void activityStepped(PActivity a) {
                                }
                            };
                            getImageLoadActivity().setDelegate(delegate);
                        } else {
                            createImage(image, delay == 0 ? 1 : delay, scale);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        getLayer().removeAllChildren();
                    }
                }
            }).start();
        } else {
            currentSong = item;
        }
    }

    private void createImage(Image image, int delay, boolean scale) {

        double scaleFactor = 1.0;

        double width = getPreferredSize().getWidth();

        if (getSize().getWidth() > width) {
            width = getSize().getWidth();
        }

        double height = getPreferredSize().getHeight();

        if (getSize().getHeight() > height) {
            height = getSize().getHeight();
        }

        if (scale) {
            if (image.getWidth(this) > width) {
                scaleFactor = (double) image.getWidth(this) / width;
            } else {
                scaleFactor = width / (double) image.getWidth(this);
            }

            if (image.getHeight(this) > height) {
                if (getSize().getHeight() / image.getHeight(this) < scaleFactor) {
                    scaleFactor = height / (double) image.getHeight(this);
                }
            } else if ((double) image.getHeight(this) / height > scaleFactor) {
                scaleFactor = (double) image.getHeight(this) / height;
            }
        }
        setSize(new Dimension((int) (image.getWidth(this) * scaleFactor), (int) ((image.getHeight(this)) * scaleFactor) + PADDING));
        setPreferredSize(new Dimension((int) (image.getWidth(this) * scaleFactor), (int) ((image.getHeight(this)) * scaleFactor) + PADDING));
        pImage = new PImage(image);
        pImage.scale(scaleFactor);
        getLayer().addChild(pImage);
        getCamera().setTransparency(0);

        getCamera().animateToTransparency(1, delay);

    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseReleased(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public SlimPlayableObject getSong() {
        return song;
    }

    private void setSong(SlimPlayableItem song) {
        this.song = song;
    }

    /**
     * @return the imageLoadActivity
     */
    public PActivity getImageLoadActivity() {
        return imageLoadActivity;
    }

    /**
     * @param imageLoadActivity the imageLoadActivity to set
     */
    public void setImageLoadActivity(PActivity imageLoadActivity) {
        this.imageLoadActivity = imageLoadActivity;
    }
}
